//
//  Tank.m
//  Tank For IPhone
//
//  Created by 奕 朱 on 11-2-25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Tank.h"
#import "Bullet.h"
#import "Constant.h"
#import "Queue.h"
#import "TankMoveListener.h"
#import "Integer.h"



@interface Tank (Private) 

- (void) fireTheadMethod: (NSDictionary *) dictionary;

@end

@implementation Tank
@synthesize barrelX;
@synthesize barrelY;
@synthesize barrelWidth;
@synthesize barrelHeight;
@synthesize preparedBulletQueue;
@synthesize firedBulletQueue;

- (id) init {

	if (self = [super init]) {
		self.direction = up;
		self.x = 0;
		self.y = 0;
		self.width = BASE_TANK_WIDTH;
		self.height = BASE_TANK_HEIGHT;
		self.barrelWidth = BASE_TANK_BARREL_WIDTH;
		self.barrelHeight = BASE_TANK_BARREL_HEIGHT;
		
		// add bullet
		preparedBulletQueue = [[Queue alloc] init];
		firedBulletQueue = [[Queue alloc] init];
		
		for (int i = 0; i < 3; i ++) {
			[preparedBulletQueue offer:[[Bullet alloc] init: self]];
		}
		
	    [self setBarrelXY];
		
		self.moveListener = [[TankMoveListener alloc] init];
	}
	
	return self;
}

- (void) draw: (CGContextRef) context {
	
	UIColor *color = [UIColor yellowColor];
	
	CGContextSetStrokeColorWithColor(context, color.CGColor);
	CGContextSetFillColorWithColor(context, color.CGColor);
	
	[self setBarrelXY];
	
	CGContextAddRect(context, CGRectMake(x, y, width, height));
	CGContextAddRect(context, CGRectMake(barrelX, barrelY, barrelWidth, barrelHeight));

	
	CGContextDrawPath(context, kCGPathFillStroke);
}



- (void) fire {
	NSLog(@"tank is firing");
	
	NSLog(@"bullet's count is %d", [self.preparedBulletQueue size]);
	
	Bullet *bullet = [preparedBulletQueue poll];
	
	if (nil == bullet) {
		return;
	}
	
	bullet.x = self.barrelX;
	bullet.y = self.barrelY;
	
	[self.firedBulletQueue offer: bullet];
	
	NSLog(@"after take bullet");
	
	NSDictionary *directory = [NSDictionary dictionaryWithObjectsAndKeys:
							  bullet, @"bullet", self.direction, @"direction", nil];
	
	NSLog(@"after directory create");

	
	NSThread *fireThread = [[NSThread alloc] initWithTarget:self 
											selector:@selector(fireTheadMethod:) 
											object: directory];
	NSLog(@"after thread create");
	
	[fireThread start];
	
}

- (void) fireTheadMethod: (NSDictionary *) dictionary {
	
	NSLog(@"fire thead start");
	
	Bullet *bullet = [dictionary objectForKey: @"bullet"];
	Integer *_direction = [dictionary objectForKey:@"direction"];
	
	

	while ([bullet move: _direction]) {
		
		NSLog(@"bullet is moving");
		
		[NSThread sleepForTimeInterval: 0.05];
		
	}
	
	[dictionary release];
	
	NSLog(@"fire thead end");

	[self.preparedBulletQueue offer: [self.firedBulletQueue poll]];
	
	[NSThread exit];
	
}


- (void) setBarrelXY {
	
	switch (self.direction.value) {
		case up: {
			self.barrelWidth = BASE_TANK_BARREL_WIDTH;
			self.barrelHeight = BASE_TANK_BARREL_HEIGHT;
			self.barrelX = self.x + self.width / 2 - self.barrelWidth / 2;
			self.barrelY = self.y - self.barrelHeight;
			break;
		}
		case down:
			self.barrelWidth = BASE_TANK_BARREL_WIDTH;
			self.barrelHeight = BASE_TANK_BARREL_HEIGHT;
			self.barrelX = self.x + self.width / 2 - self.barrelWidth / 2;
			self.barrelY = self.y + self.width;
			break;
		case left:
			self.barrelWidth = BASE_TANK_BARREL_HEIGHT;
			self.barrelHeight = BASE_TANK_BARREL_WIDTH;
			self.barrelX = self.x - self.barrelWidth;
			self.barrelY = self.y + self.height / 2 - self.barrelHeight / 2;
			break;
		case right:
			self.barrelWidth = BASE_TANK_BARREL_HEIGHT;
			self.barrelHeight = BASE_TANK_BARREL_WIDTH;
			self.barrelX = self.x + self.width;
			self.barrelY = self.y + self.height / 2 - self.barrelHeight / 2;
			break;
	}
	
	
}

- (void) dealloc {
	
	[preparedBulletQueue release];
	[firedBulletQueue release];
	[super dealloc];

}

@end
